﻿namespace FredCK.FCKeditorV2.FileBrowser
{
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web.Security;

    public class DBHelper
    {
        private static SqlConnection connection;

        public static SqlDataAdapter dataAdapter(string sql)
        {
            return new SqlDataAdapter(sql, Connection);
        }

        public static SqlDataReader dataReader(string sql)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            return command.ExecuteReader();
        }

        public static SqlDataReader dataReader(string sql, SqlParameter[] par)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            command.Parameters.AddRange(par);
            command.CommandType = CommandType.StoredProcedure;
            return command.ExecuteReader();
        }

        public static string Decrypt(string Text)
        {
            return Decrypt(Text, "elingsoft");
        }

        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            int num = Text.Length / 2;
            byte[] buffer = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte) num3;
            }
            provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Encoding.Default.GetString(stream.ToArray());
        }

        public static void ExecuteNonQuery(string sql)
        {
            new SqlCommand(sql, Connection).ExecuteNonQuery();
        }

        public static void ExecuteNonQuery(string sql, SqlParameter[] par)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            command.Parameters.AddRange(par);
            command.CommandType = CommandType.StoredProcedure;
            command.ExecuteNonQuery();
        }

        public static int ExecuteNonQuery1(string sql)
        {
            SqlCommand command = new SqlCommand(sql, Connection);
            return command.ExecuteNonQuery();
        }

        public static string getConnectionString()
        {
            string text = ConfigurationManager.AppSettings["ConnectionString"];
            string str2 = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (str2 == "true")
            {
                text = Decrypt(text);
            }
            return text;
        }

        public static SqlConnection Connection
        {
            get
            {
                string connectionString = getConnectionString();
                if (connection == null)
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }
    }
}

